<template>
  <div class="bottom-cart">
    <transition name="scroll">
      <div class="list" v-show="showList">
        <div class="title">
          <span class="left">购物车</span>
          <span class="right" @click="clearCart">清空</span>
        </div>
        <ul>
          <li v-for="(food, index) in cartFoods" :key="index">
            <span class="food-name">{{ food.name }}</span>
            <div class="right">
              <span class="unit-price">￥{{ food.price }}</span>
              <div class="cart-control-wrapper">
                <CartControl :food="food"></CartControl>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </transition>

    <div class="bar" @click="toggleShow">
      <div class="left">
        <div class="cart-logo" :class="{ highlight: totalCount }">
          <i class="iconfont icon-add-cart"></i>
          <span class="total-num" v-show="totalCount">{{ totalCount }}</span>
        </div>
        <div class="price">
          <h3 class="total-price">￥{{ totalPrice }}</h3>
          <p class="delivery-fee">另需配送费￥4 元</p>
        </div>
      </div>
      <div class="right" :class="{ enough: totalPrice >= 20 }">{{ buttonText }}</div>
    </div>

    <transition name="fade">
      <div class="cover-layer" v-show="showList" @click="showList = false"></div>
    </transition>
  </div>
</template>

<script>
import CartControl from "../CartControl/CartControl.vue"

import { mapState, mapGetters } from "vuex"
export default {
  data() {
    return {
      showList: false,
    }
  },
  computed: {
    ...mapState(["cartFoods"]),
    ...mapGetters(["totalCount", "totalPrice"]),
    buttonText() {
      if (this.totalPrice === 0) {
        return "￥20元起送"
      } else if (this.totalPrice < 20) {
        return `还差￥${20 - this.totalPrice}起送`
      } else {
        return "去结算"
      }
    },
  },
  watch: {
    //当购物车里没有东西时,购物车列表就要隐藏
    cartFoods(value) {
      if (value.length === 0) {
        this.showList = false
      }
    },
  },
  methods: {
    clearCart() {
      this.$store.dispatch("clearCart")
    },
    toggleShow() {
      // 如果showList为false,需要变为true,但是变之前要检查购物车里面有没有东西, 购物车不为空才允许列表弹出
      if (!this.showList && this.cartFoods.length > 0) {
        this.showList = true
      } else if (this.showList) {
        this.showList = false
      }
    },
  },
  components: {
    CartControl,
  },
}
</script>

<style lang="less" scoped>
@import "../../common/style/mixin.less";
.bottom-cart {
  position: fixed;
  bottom: 0;
  left: 0;
  // 该子组件根元素的z-index默认为0, 所以就算内部的元素z-index设的很高, 依然覆盖不了其他组件具有z-index属性的元素
  z-index: 10;
  width: 100%;
  min-width: 320px;
  max-width: 750px;
  height: 4.8rem;
  .bar {
    background-color: #141d27;
    position: relative;
    z-index: 100;
    display: flex;
    height: 100%;
    justify-content: space-between;
    .left {
      .cart-logo {
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        bottom: 0.2rem;
        left: 1.2rem;
        width: 5.6rem;
        height: 5.6rem;
        border: 0.6rem solid #141d27;
        border-radius: 50%;
        background-color: #2b343c;
        i {
          font-size: 3rem;
          color: #80858a;
        }
        .total-num {
          display: none;
          position: absolute;
          top: -0.5rem;
          right: -0.6rem;
          width: 2.4rem;
          height: 1.6rem;
          line-height: 1.7rem;
          text-align: center;
          border-radius: 1.6rem;
          font-size: 1rem;
          font-weight: 700;
          color: #fff;
          background: #f01414;
        }
        &.highlight {
          background-color: @theme-color;
          i {
            color: #fff;
          }
          .total-num {
            display: block;
          }
        }
      }
      .price {
        margin-left: 8rem;
        .total-price {
          font-size: 1.6rem;
          font-weight: 700;
          color: #fff;
          margin-top: 0.9rem;
        }
        .delivery-fee {
          font-size: 1rem;
          color: #8d939b;
          margin-top: 0.4rem;
        }
      }
    }
    .right {
      width: 10.5rem;
      line-height: 4.8rem;
      text-align: center;
      font-size: 1.2rem;
      font-weight: 700;
      color: #fff;
      background-color: #2b333b;
      &.enough {
        background-color: @theme-color;
        color: #fff;
      }
    }
  }
  .list {
    position: absolute;
    z-index: 90;
    bottom: 4.8rem;
    width: 100%;
    max-height: 50vh;
    overflow: hidden;
    .title {
      display: flex;
      justify-content: space-between;
      height: 4rem;
      line-height: 4rem;
      padding: 0 1.8rem;
      background: #f3f5f7;
      border-bottom: 0.1rem solid rgba(7, 17, 27, 0.1);
      .left {
        font-size: 1.4rem;
        color: #07111b;
      }
      .right {
        font-size: 1.2rem;
        color: #00a0dc;
      }
    }
    & > ul {
      li {
        display: flex;
        justify-content: space-between;
        height: 4.8rem;
        line-height: 2.4rem;
        padding: 1.2rem 1.8rem;
        background-color: #ffffff;
        .bottom-border-1px(#e6e7e8);
        &::after {
          width: 90%;
          left: 50%;
          transform: translateX(-50%);
        }
        .food-name {
          font-size: 1.4rem;
          color: #07111b;
        }
        .right {
          display: flex;
          .unit-price {
            font-size: 1.4rem;
            color: #f01414;
            margin-right: 1.3rem;
          }
          .cart-control-wrapper {
            margin-right: 0.6rem;
          }
        }
      }
    }
  }
  .cover-layer {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.3);
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: 0.35s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}

.scroll-enter-active,
.scroll-leave-active {
  transition: 0.35s;
}
.scroll-enter,
.scroll-leave-to {
  transform: translateY(100%);
}
</style>
